﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using CCWin;
namespace weiqijiance
{
    public partial class JDspeed : CCSkinMain
    {
        public mainform m_mainform;

        public Thread t1;
        public Thread t2;

        public float fli;
        public float fspeed;
        public bool btingzhi;
        public int ipinglv;
        public string sdengpaiSH;
        public string sdengpaiXH;
        public float flilingwei;

        public float DIW;
        public float m_fspeed;
        public float fForce;

        public string sASMJGXZ;//是否进行结果修正
        public int iASMtdz;//ASM信号左通道
        public int iASMtdy;//ASM信号右通道
        public int iASMtdh;//ASM信号右通道
        public int iASMHWQ;//ASM到位红外

        public int iASMKZJSS;//ASM举升升
        public int iASMKZJSJ;//ASM举升降
        public int iASMSDtd;//车速信号接在那个通道
        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒


        public JDspeed()
        {
            InitializeComponent();
           
        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        private delegate void FlushClient();//代理
        private void xianshiLI()
        {
            if (this.text_li.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiLI);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_li.Text = string.Format(@"{0}", fli);
            }
        }

        private void xianshiSpeed()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiSpeed);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_sudu.Text = string.Format(@"{0}", fspeed.ToString("0.00"));
            }
        }
        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            ///读取检定数据

            for (int i = 0; i < 10; i++)
            {
                string sgetname = m_mainform.ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iASMtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iASMtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iASMtdh = i;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                string smaichong = m_mainform.ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("速度"))
                {
                    iASMSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("测功机到位"))
                {
                    iASMHWQ = i;
                }


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iASMKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iASMKZJSJ = i;
                }
            }
        }
        public void Thread1()
        {
            while(true)
            {
                Thread.Sleep(100);
                jianding();
                xianshiLI();
                xianshiSpeed();
                if (btingzhi)
                {
                    break;
                }
            }
        }
        public void Thread2()
        {
            //DateTime d1000 = DateTime.Now;
            //for (; ; )
            //{
            //    for (; ; )
            //    {
            //        if ((DateTime.Now - d1000).TotalMilliseconds > 1000)
            //            break;
            //    }
            //    d1000 = DateTime.Now;

            //}


        }
        public void jianding()
        {
            Thread.Sleep(20);
            m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
            float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
            float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
            float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

            fForce = fzou + fyou + fhou;
            if (fForce < 0)
            {
                fForce = 0;
            }

           fspeed= m_fspeed;
           fli = fForce * 10;
         
           sdengpaiSH = string.Format("速度:{0} km/h", fspeed.ToString("0.00"));
           sdengpaiXH = string.Format("力:{0} N", fli.ToString("0.00"));
           m_mainform.flowwq.LedWrite(sdengpaiSH, sdengpaiXH, 1);
        }

        private void but_kaishi_Click(object sender, EventArgs e)
        {
            but_kaishi.Enabled = false;
            m_mainform.DAC.tiaoling();//调零
            m_mainform.DAC.Lift(false);
            btingzhi = false;
            flilingwei = m_mainform.m_fForce;
            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();            
        }

        private void but_tiaoling_Click(object sender, EventArgs e)
        {
            //m_mainform.DAC.tiaoling();
            flilingwei = m_mainform.m_fForce;
        }

        private void but_tingzhi_Click(object sender, EventArgs e)
        {
            but_kaishi.Enabled = true;
            btingzhi = true;
            t1.Abort();
            t1.DisableComObjectEagerCleanup();

            t2.Abort();
            t2.DisableComObjectEagerCleanup();
          
        }
        bool bbpqkg = false;
        private void but_bqpks_Click(object sender, EventArgs e)
        {
            bbpqkg = true;
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(true);
            ipinglv = 256 * 1;
            Thread.Sleep(3000);
            m_mainform.BianPinQiSpeed(ipinglv);//设置频率 256 对应2.56
        }

        private void but_bqpgb_Click(object sender, EventArgs e)
        {
            bbpqkg = false;
            m_mainform.BianPinQi(false);
        }  

        private void but_sdzj_Click(object sender, EventArgs e)
        {
            ipinglv = ipinglv + 256 * 1;
            m_mainform.BianPinQiSpeed(ipinglv);//设置频率 256 对应2.56
        }

        private void but_sdjd_Click(object sender, EventArgs e)
        {
            ipinglv = ipinglv - 256 * 1;
            m_mainform.BianPinQiSpeed(ipinglv);//设置频率 256 对应2.56
        }

        private void JDspeed_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }

        private void JDspeed_Load(object sender, EventArgs e)
        {

            gettongdao();
            sdengpaiSH = "";
            sdengpaiXH = "";
            ipinglv = 0;
            fli = 0;
            fspeed = 0;
            btingzhi = false;

            //t1 = new Thread(new ThreadStart(Thread1));
            t2 = new Thread(new ThreadStart(Thread2));
            t2.Priority = ThreadPriority.Lowest;
            t2.Start();
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            x = this.Width;
            y = this.Height;
            setTag(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bbpqkg)
            {
                MessageBox.Show("变频器目前状态是打开的，需要关闭再发送码值");
                return;
            }
            double dwlkz=0;
            double.TryParse(txtwlkz.Text,out dwlkz);
            m_mainform.DAC.PutVol(dwlkz);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);
            float.TryParse(m_mainform.ReadValue("检测设备信息", "台架惯量"), out DIW);

            Random sj = new Random();
            string bdnr = string.Format(@"
            <hxsj1>{0}</hxsj1>
            <jsgl1>{1}</jsgl1>
            <sdzd1>{2}</sdzd1>
            <sdzx1>{3}</sdzx1>
            <mysd1>{4}</mysd1>
            <jbgl>{5}</jbgl>
            <kssj1>{6}</kssj1>
            <jssj1>{7}</jssj1>",
            33 + sj.Next(100, 200) * 0.001,
            0.8 + sj.Next(1, 10) * 0.01,
            54 + sj.Next(2, 5),
            46 + sj.Next(2, 6),
            8 + sj.Next(3, 5),
            DIW,
            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            DateTime.Now.AddMinutes(9).ToString("yyyy-MM-dd HH:mm:ss"));//fwj20190603
            string vehispara = "";
            string stoubao = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
            stoubao = stoubao + string.Format(@"<root><head><organ>{0}</organ><jkxlh>{1}</jkxlh><jkid>{2}</jkid></head>",
                                                           m_mainform.organ,
                                                          m_mainform.jkxlh,
                                                           "BD000");
            vehispara = stoubao + string.Format(@"<body><vehispara>
                                <sbbh>{0}</sbbh>
                                <bdsj>{1}</bdsj>
                                <bdr>{2}</bdr>
                                <czr>{3}</czr>
                                <bdlx>{4}</bdlx>
			                    <bdnr>{5}</bdnr>
			                    <bdjg>{6}</bdjg>
			                    <tsno>{7}</tsno>
			                    <testlineno>{8}</testlineno>
                                 </vehispara>
			                    </body>
                                </root>",
                                   m_mainform.sdpcgjsbbh,
                                   DateTime.Now.AddMinutes(-5).ToString("yyyy-MM-dd HH:mm:dd"),
                                   m_mainform.encode("蒋佩"),
                                   m_mainform.encode("蒋佩"),
                                   "03",
                                   bdnr,
                                   "1",
                                   m_mainform.stationCode,
                                    m_mainform.lineCode
                                  );



            WebReferenceCS.VeptsOutAccess m_vesd = new WebReferenceCS.VeptsOutAccess();
            m_vesd.Url = m_mainform.surl;

            MessageBox.Show(vehispara);
            string sgc005 = m_vesd.write(vehispara);
            MessageBox.Show(m_mainform.decode(sgc005));
        }

        private void button8_Click(object sender, EventArgs e)
        {
            MessageBox.Show(m_mainform.objbass.uploadjdsd());
            MessageBox.Show(m_mainform.objbass.uploadjdli());
        }
    }
}
